import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_widgets_pc/widgets/gesture/kq_gesture_detector.dart';

/// 图表基类
abstract class BaseChart<D, G extends BaseChartGestureHandler>
    extends StatefulWidget {
  /// 动画时间，设置此值则启用动画绘制
  final Duration? animDuration;

  /// 表格手势处理
  final G? gestureHandler;

  /// 当[isEmptyData]返回true时显示的内容
  final Widget Function()? emptyWidgetBuilder;

  /// 表格绘制器
  final CustomPainter Function(BuildContext context, double animProgress)?
      painterBuilder;

  /// 图表数据
  final D? data;

  /// 基础表格
  ///
  /// [painterBuilder]为表格内容绘制器，[animDuration]控制动画绘制时间，不设置则无动画。
  /// [gestureHandler]处理表格的手势，[emptyWidgetBuilder]为空数据时显示的内容，不设置
  /// 此值且无数据时，则依然执行绘制逻辑，具体显示内容由实际绘制器呈现
  const BaseChart(
      {super.key,
      this.data,
      this.painterBuilder,
      this.animDuration,
      this.gestureHandler,
      this.emptyWidgetBuilder});

  /// 判断当前是否是空数据
  bool isEmptyData();
}

/// 科强表格[State]基类
abstract class BaseChartState<D, G extends BaseChartGestureHandler,
    C extends BaseChart<D, G>> extends State<C> with TickerProviderStateMixin {
  /// 控制绘制动画
  AnimationController? _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _animationController?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _animationController?.stop(canceled: true);
    var builder = widget.emptyWidgetBuilder;
    if (widget.isEmptyData() && builder != null) {
      return builder.call();
    }

    return LayoutBuilder(builder: (context, constraint) {
      return KqGestureDetector(
          onDragDown: widget.gestureHandler?.onDragDown,
          onDragStart: widget.gestureHandler?.onDragStart,
          onDragUpdate: widget.gestureHandler?.onDragUpdate,
          onDragEnd: widget.gestureHandler?.onDragEnd,
          onDragCancel: widget.gestureHandler?.onDragCancel,
          child: SizedBox.expand(
            child: _buildContent(context, constraint),
          ));
    });
  }

  Widget _buildContent(BuildContext context, BoxConstraints constraints) {
    return widget.animDuration == null || widget.isEmptyData()
        ? buildChild(context, 1, constraints)
        : _withAnim(widget.animDuration!, constraints);
  }

  Widget _withAnim(Duration duration, BoxConstraints constraints) {
    _animationController?.duration = duration;
    var widget = AnimatedBuilder(
      animation: _animationController!,
      builder: (context, child) {
        return buildChild(
            context, _animationController?.value ?? 1, constraints);
      },
    );
    _animationController?.forward();
    return widget;
  }

  @protected
  Widget buildChild(
      BuildContext context, double animProgress, BoxConstraints constraints) {
    return CustomPaint(painter: buildPainter(context, animProgress));
  }

  /// 构建绘制器
  ///
  /// [animProgress]表示动画进程，0~1,默认无动画或动画结束时为1
  @protected
  CustomPainter buildPainter(BuildContext context, double animProgress);

  /// 刷新界面
  void update() {
    setState(() {});
  }
}

/// 手势事件处理
mixin BaseChartGestureHandler {
  /// 拖拽开始，此事件发生在手指按下首次滑动
  void onDragStart(DragStartDetails details);

  /// 拖拽按下，此事件发生在手指首次触摸屏幕
  void onDragDown(DragDownDetails details);

  /// 拖拽更新，此事件发生在手指持续滑动过程
  void onDragUpdate(DragUpdateDetails details);

  /// 拖拽结束，此事件发生在手指持续滑动后抬起
  void onDragEnd(DragEndDetails details);

  /// 拖拽取消，此事件发生在手指按下但未发生任何滑动时执行
  void onDragCancel();
}
